package com.sdtele.tta.operation;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.sdtele.tta.dac.Dac;
import com.sdtele.tta.models.JsonRequest;
import com.sdtele.tta.models.JsonResponse;
import com.sdtele.tta.models.User;
import com.sdtele.tta.utils.TeamUtils;

/**
 * 操作类实现：GetUser(json.operation=getUser)
 * 
 * @author XCream
 */
public class OperationResetPasswd extends Operation {

	@Override
	public JsonResponse handle(JsonRequest jsonRequest) {
		JsonResponse jres = new JsonResponse();
		User curUser = (User) request.getSession().getAttribute("user");
		if (curUser != null) {
			long id = jsonRequest.getJsonObject().get("userId").getAsLong();
			User user = Dac.getInstance().getUser(id);
			String newPasswd = jsonRequest.getJsonObject().get("newPasswd").getAsString();
			long teamId = user.getTeamId();
			if (curUser.getIsManager() == 1) {
				Set<Long> teamIdSet = new HashSet<Long>();
				teamIdSet.add(curUser.getTeamId());
				TeamUtils.teamAuthority(curUser.getTeamId(), teamIdSet);
				if (!teamIdSet.contains(teamId)) {
					jres.setStatus(1);
					jres.setMessage("您的管理权限不允许重置该员工密码");
				} else {
					user.setPasswd(newPasswd);
					if (Dac.getInstance().chgUser(user)) {
						jres.setStatus(0);
						jres.setMessage("重置密码成功");
						jres.setEntity(user);
					} else {
						jres.setStatus(1);
						jres.setMessage("重置密码失败");
					}
				}
			} else {
				jres.setStatus(1);
				jres.setMessage("您不是管理员，不能重置其他人的密码");
			}
		}
		return jres;
	}

}
